// Slider on homepage

jQuery(document).ready(function(){
    jQuery("#home_slider ul.vertical-list li").click(function(){
        var item=jQuery(this);
        if (item.hasClass("previous") || item.hasClass("next"))
        {
            var index=jQuery("#home_slider ul.vertical-list li.active").index();
            if (item.hasClass("previous"))
                index--;
            else
                index++;
            if (index ==0)
                index=jQuery("#home_slider ul.vertical-list li").length-2;
                
            if (index >= jQuery("#home_slider ul.vertical-list li").length-1)
                index=1;
            jQuery("#home_slider ul.vertical-list li").eq(index).trigger("click");
        }
        else
        {
            var container =jQuery("#home_slider div.desc-container");
            // get current slide
            var current_slide_index = container.attr("slide_index");
            if (!(current_slide_index >0))
                current_slide_index =0;
            // get index of current processing slide
            var _slide_index=jQuery(this).index();
            
            // if same slide index -> do nothing
            if (current_slide_index == _slide_index)
            {
                return;
            }
            //
            var slide1=jQuery(container.children()[current_slide_index-1]);
            var slide2=jQuery(container.children()[_slide_index-1]);
            var image_container = jQuery("#home_slider .image-full");
            var slide1_image=image_container.children().eq(current_slide_index-1);
            var slide2_image=image_container.children().eq(_slide_index-1);
        
            // set top of slide1 first
            slide2.css("top",container.height()+"px").css("opacity","0").css("visibility","visible");
            slide2.animate({
                opacity: 1,
                top:"0px"
                }, 500, function(){
                    slide2.css("top","0px").css("opacity","1").css("visibility","visible");
                });
            // fadeOut
            slide1.animate({
                opacity: 0,
                }, 400, function() {
                    jQuery(this).css("visibility","hidden");
                });
            
                
            slide2_image.css("top",image_container.height()+"px").css("opacity","0").css("visibility","visible");
            slide2_image.animate({
                opacity: 1,
                top:"0px"
                }, 500, function(){
                    slide2_image.css("top","0px").css("opacity","1").css("visibility","visible");
                });
            // fadeOut
            slide1_image.animate({
                opacity: 0,
                }, 400, function() {
                    jQuery(this).css("visibility","hidden");
                });
            
            
            // update
            jQuery("#home_slider ul.vertical-list li").removeClass("active");
            jQuery(this).addClass("active");
            container.attr("slide_index",_slide_index);
        }
    });
    
    // trigger click first show
    jQuery("#home_slider div.desc-container > div:first").css("top","0px").css("opacity","1").css("visibility","visible");
    jQuery("#home_slider .image-full img:first").css("top","0px").css("opacity","1").css("visibility","visible");
    jQuery("#home_slider div.desc-container").attr("slide_index",1);
});